package com.leo.common.security.server.handler;

import cn.hutool.http.HttpUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.nio.charset.Charset;

/**
 * <p>
 * UserFormAuthenticationFailureHandler
 * </p>
 *
 * @author ：Leo
 * @since ：2021-04-06 16:28
 */
@Slf4j
public class UserFormAuthenticationFailureHandler implements AuthenticationFailureHandler {

	@Override
	public void onAuthenticationFailure(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse,
			AuthenticationException e) throws IOException, ServletException {
		log.debug("表单登录失败:{}", e.getLocalizedMessage());

		httpServletResponse.sendRedirect(String.format("/token/login?error=%s",
				HttpUtil.encodeParams(e.getMessage(), Charset.defaultCharset())));
	}

}
